package net.qanar.gwnewsfeed.reader;

import java.util.*;

import net.qanar.gwnewsfeed.*;

import org.jdom.*;
import org.jdom.input.*;

public class RSSCommunityReader extends Reader {
	private static String location = "http://guildwars.com/community/rss/community-current.xml";

	@Override
	public Collection<Item> getItems() {
		Collection<Item> items = new ArrayList<Item>();
		
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(location);
			
			List<Element> updates = document.getRootElement().getChild("channel").getChildren("item");
			
			for (Element e : updates) {
				String title = e.getChild("title").getText().trim();
				String content = e.getChild("description").getText().trim();
				String source = getName();
				Date date = new Date(e.getChild("pubDate").getText().trim());				
				
				Item i = new Item(title, content, source, date);
				items.add(i);			
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return items;
	}

	@Override
	public String getName() {
		return "Guild Wars Community News";
	}
}